unit nomenclatureselect;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, Buttons,
  DBGrids, StdCtrls, ExtCtrls, Grids;

type

  { TfrmNomenclatureSelect }

  TfrmNomenclatureSelect = class(TForm)
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    BitBtn3: TBitBtn;
    BitBtn4: TBitBtn;
    btnEdit: TBitBtn;
    btnAddGroup: TBitBtn;
    btnCreate: TBitBtn;
    DBGNomenclature: TDBGrid;
    GroupBox1: TGroupBox;
    LabeledEdit1: TLabeledEdit;
    LabeledEdit2: TLabeledEdit;
    LabeledEdit3: TLabeledEdit;
    procedure BitBtn3Click(Sender: TObject);
    procedure btnAddGroupClick(Sender: TObject);
    procedure btnCreateClick(Sender: TObject);
    procedure DBGNomenclatureDblClick(Sender: TObject);
    procedure DBGNomenclatureDrawColumnCell(Sender: TObject; const Rect: TRect;
      DataCol: Integer; Column: TColumn; State: TGridDrawState);
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure GetGroupTitle;
    procedure selectNomenclature;
  private
    { private declarations }
  public
    { public declarations }
    CurrentGroupRecordNumber:Integer;
    price:Currency;
    priceretail:Integer;
    pricesmallwholesale:Integer;
    pricebigwholesale:Integer;
  end;

var
  frmNomenclatureSelect: TfrmNomenclatureSelect;
  LastRecNo:String;
  groupTitle:String;
implementation

{$R *.lfm}

{ TfrmNomenclatureSelect }
uses main,nomenclaturecreateedit,nomenclaturecreateeditgroup,ZAbstractRODataset;

procedure TfrmNomenclatureSelect.GetGroupTitle;
begin
  groupTitle:='';
  if (CurrentGroupRecordNumber<>0)  then begin
     FrmMain.ZQuery.Active:=False;
     FrmMain.ZQuery.SQL.Text:='SELECT title FROM nomenclature WHERE id='+IntToStr(CurrentGroupRecordNumber);
     FrmMain.ZQuery.Active:=True;
     groupTitle:=FrmMain.DSQuery.DataSet.FieldByName('title').AsString;
  end;
  frmNomenclatureCreateEdit.edtGroup.Text:=groupTitle;
  frmNomenclatureCreateEditGroup.edtGroup.Text:=groupTitle;
  FrmMain.ZQuery.Active:=False;
end;

procedure TfrmNomenclatureSelect.selectNomenclature;
begin
  if (DBGNomenclature.DataSource.DataSet.FieldByName('group').AsBoolean=False) then begin
    FrmMain.nomenclatureSelectedId:=DBGNomenclature.DataSource.DataSet.FieldByName('id').AsInteger;
    FrmMain.nomenclatureSelectedTitle:=DBGNomenclature.DataSource.DataSet.FieldByName('title').AsString;
    price:=DBGNomenclature.DataSource.DataSet.FieldByName('price').AsCurrency;
    priceretail:=DBGNomenclature.DataSource.DataSet.FieldByName('priceretail').AsInteger;
    pricesmallwholesale:=DBGNomenclature.DataSource.DataSet.FieldByName('pricesmallwholesale').AsInteger;
    pricebigwholesale:=DBGNomenclature.DataSource.DataSet.FieldByName('pricebigwholesale').AsInteger;
    Close;
  end;
end;

procedure TfrmNomenclatureSelect.btnCreateClick(Sender: TObject);
begin
  GetGroupTitle;
  FrmMain.DSNomenclature.DataSet.Append;
  FrmMain.DSNomenclature.DataSet.FieldByName('quantity').AsInteger:=0;
  frmNomenclatureCreateEdit.ShowModal;
end;

procedure TfrmNomenclatureSelect.DBGNomenclatureDblClick(Sender: TObject);
var
  id,parentid:String;
  group:Boolean;
begin
  id:=DBGNomenclature.DataSource.DataSet.FieldByName('id').AsString;
  parentid:=DBGNomenclature.DataSource.DataSet.FieldByName('parentid').AsString;
  group:=DBGNomenclature.DataSource.DataSet.FieldByName('group').AsBoolean;
  if (group)  AND (LastRecNo=id) then begin
    FrmMain.ZTNomenclature.Filtered:=False;
    FrmMain.ZTNomenclature.SortedFields:='id,group,title';
    FrmMain.ZTNomenclature.SortType:=stAscending;
    FrmMain.ZTNomenclature.Filter:='id='+parentid+' or parentid='+parentid;
    FrmMain.ZTNomenclature.Filtered:=True;
    LastRecNo:=parentid;
    CurrentGroupRecordNumber:=StrToInt(parentid);
  end else begin
  if ((group) AND (parentid<>id))
  then begin
    FrmMain.ZTNomenclature.Filtered:=false;
    FrmMain.ZTNomenclature.SortedFields:='id,group,title';
    FrmMain.ZTNomenclature.SortType:=stAscending;
    FrmMain.ZTNomenclature.Filter:='id='+id+' or parentid='+id;
    FrmMain.ZTNomenclature.Filtered:=true;
    LastRecNo:=id;
    CurrentGroupRecordNumber:=StrToInt(id);
  end;
  end;
  if (NOT group) then begin
    selectNomenclature;
  end;
end;

procedure TfrmNomenclatureSelect.DBGNomenclatureDrawColumnCell(Sender: TObject;
  const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
var
bmp: TBitmap;
begin
    if (Column.FieldName = 'id') and (DBGNomenclature.DataSource.DataSet.FieldByName('group').AsBoolean) then begin
       bmp := TBitmap.Create;
       if (Column.Field.AsString=LastRecNo) then begin
         FrmMain.ImageList2.GetBitmap(3,bmp);
       end else begin
         FrmMain.ImageList2.GetBitmap(0,bmp);
       end;
       DBGNomenclature.Canvas.Draw(Rect.Left,Rect.Top,bmp);
       bmp.Free;
    end else if (Column.FieldName = 'id') then begin
       bmp := TBitmap.Create;
       FrmMain.ImageList2.GetBitmap(1,bmp);
       DBGNomenclature.Canvas.Draw(Rect.Left,Rect.Top,bmp);
       bmp.Free;
    end;
end;

procedure TfrmNomenclatureSelect.FormCreate(Sender: TObject);
begin
  FrmMain.ZTNomenclature.Filtered:=False;
  FrmMain.ZTNomenclature.SortedFields:='id,group,title';
  FrmMain.ZTNomenclature.SortType:=stAscending;
  FrmMain.ZTNomenclature.Filter:='parentid=0';
  FrmMain.ZTNomenclature.Filtered:=True;
  CurrentGroupRecordNumber:=0;
end;

procedure TfrmNomenclatureSelect.FormShow(Sender: TObject);
begin
  FrmMain.DSNomenclature.DataSet.Refresh;
end;

procedure TfrmNomenclatureSelect.btnAddGroupClick(Sender: TObject);
begin
  GetGroupTitle;
  frmNomenclatureCreateEditGroup.edtGroupTitle.Text:=EmptyStr;
  frmNomenclatureCreateEditGroup.isNew:=True;
  frmNomenclaturecreateeditgroup.ShowModal;
end;

procedure TfrmNomenclatureSelect.BitBtn3Click(Sender: TObject);
begin
  selectNomenclature;
end;

end.

